package com.sali.commons.config;

import com.sali.service.data_processor.ProcessThread;

import java.util.concurrent.*;

// 线程池维护类
public class ThreadPoolManager {


    // 执行指标计算
    private static ScheduledExecutorService scheduledExecutor;

    private static ThreadPoolExecutor realtimeThreadPoolExecutor;

    public static void initScheduledExecutor() {
        ThreadFactory scheduledThreadFactory = new ThreadNamedFactory("sali-scheduled-");
        scheduledExecutor = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors() * 2, scheduledThreadFactory);
    }

    public static void shutdownScheduledExecutor() {
        scheduledExecutor.shutdown();
    }

    public static void setRealtimeThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) {
        realtimeThreadPoolExecutor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
    }

    public static void executeRealtimeThreadPool(ProcessThread processThread) {
        realtimeThreadPoolExecutor.execute(processThread);
    }

    public static ScheduledExecutorService getScheduledExecutor() {
        return scheduledExecutor;
    }
}
